package com.xingkeduo.dto.transter.marketingdetail;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.utils.DateUtils;
import com.xingkeduo.utils.WeekUtil;

import lombok.Data;

@Data
public class MarketingDetail {
	private String id;//券，卡ID
	private String marketing_type;//类型
	private String title;//券，卡名称
	private String use_store;//适用门店
	private String date;//使用时段
	private String start_date;//使用开始日期
	private String end_date;//使用截止日期
	private String card_type;//现金，折扣，特价。。。
	private String use_type;//0 相对时间，1 固定时间
	private List<String> reduce_cost;
	private List<String> discount;
	private String type;
	private String least_cost;
	private String refund_type;//是否退款退券（0 退款退券，1 退款不退券）
	private String servprod_name;//支持的服务名称
	private String period;//期限
	private String price;//面额
	private String service_type;//类型
	private String unreason;
	private int tips_status;
	private String marketing_count;
	private String use_memo;
	private String coupon_bg;
	
	public void covert(JSONObject json) {
		this.id = json.getOrDefault("id", "")==null?"":json.getOrDefault("id", "").toString();
		this.title = json.getOrDefault("name", "")==null?"":json.getOrDefault("name", "").toString();
		this.marketing_type = json.getOrDefault("marketingType", "")==null?"":json.getOrDefault("marketingType", "").toString();
		//this.use_store = json.getOrDefault("storeNames", "")==null?"":json.getOrDefault("storeNames", "").toString();
		String storeIds = json.getOrDefault("storeIds", "")==null?"":json.getOrDefault("storeIds", "").toString();
		this.use_store="适用部分门店";
		if("0".equals(storeIds)) {
			this.use_store="适用所有门店";
		}
		this.use_type = json.getOrDefault("useType", "")==null?"":json.getOrDefault("useType", "").toString();
		this.price = json.getOrDefault("couponAmount", "")==null?"":json.getOrDefault("couponAmount", "").toString();
		this.service_type = json.getOrDefault("serviceType", "")==null?"":json.getOrDefault("serviceType", "").toString();
		this.use_memo=json.getOrDefault("useMomo", "")==null?"":json.getOrDefault("useMomo", "").toString();
		String works = json.getOrDefault("weekHourIds", "")==null?"":json.getOrDefault("weekHourIds", "").toString();
		String bannerUrl = json.getOrDefault("bannerUrl", "")==null?"":json.getOrDefault("bannerUrl", "").toString();
		String bannerPath = json.getOrDefault("bannerPath", "")==null?"":json.getOrDefault("bannerPath", "").toString();
		if(StringUtils.isBlank(bannerUrl) || StringUtils.isBlank(bannerPath)) {
			coupon_bg = "http://mcdn.xingkeduo.com/image/activity/get_coupon_banner.png";
		}else {
			coupon_bg = bannerPath+bannerUrl;
		}
		if (StringUtils.isBlank(works) || "0".equals(works)) {
			this.date = "周一至周日 0-24点";
		} else {
			JSONArray array = json.getJSONArray("activityDays");
			if (array != null && array.size() > 0) {
				StringBuffer daysInfo = new StringBuffer();
				int size = array.size();
				for(int i=0;i<size;i++) {
					JSONObject o = array.getJSONObject(i);
					String daysIds = o.getOrDefault("dayids", "")==null?"":o.getOrDefault("dayids", "").toString();
					if (StringUtils.isBlank(daysIds)) {
						continue;
					}
					String startTime = o.getOrDefault("startTime", "").toString();
					int start = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(startTime)));
					String endTime = o.getOrDefault("endTime", "").toString();
					int end = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(endTime)));
					if (daysIds.startsWith(",")) {
						daysIds = daysIds.substring(1, daysIds.length());
					}
					if (daysIds.endsWith(",")) {
						daysIds = daysIds.substring(0, daysIds.length() - 1);
					}
					daysInfo.append(WeekUtil.convertWeek(daysIds)).append(" ")
							.append(start).append("-")
							.append(end).append("点; ");
				}
				if(StringUtils.isBlank(daysInfo.toString())) {
					this.date ="周一至周日 0-24点";
				}else {
					this.date =daysInfo.toString();
				}
			}else {
				this.date ="周一至周日 0-24点";
			}
		}
		Date startDate = json.getDate("useStartTime");
		if (startDate != null) {
			this.start_date = DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT_V3);
		}
		Date endDate = json.getDate("useEndTime");
		Integer usePeriod = json.getOrDefault("usePeriod", "0")==null?0:Integer.parseInt(json.getOrDefault("usePeriod", "0").toString());
		this.period = usePeriod.toString();
		if (endDate != null) {
			this.end_date = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);
		}else{
			this.end_date = DateUtils.formatDate(DateUtils.addSomeDays(startDate, usePeriod), DateUtils.DATE_FORMAT_V3);
		}
		this.card_type = json.getOrDefault("serviceType", "")==null?"":json.getOrDefault("serviceType", "").toString();
		this.type = "1";
		BigDecimal leastAmount = new BigDecimal(json.getOrDefault("leastCost", "0")==null?"0":json.getOrDefault("leastCost", "0").toString());
		leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
		this.least_cost = leastAmount.toString();
		this.refund_type=json.getOrDefault("refundType", "")==null?"":json.getOrDefault("refundType", "").toString();
		this.servprod_name=json.getOrDefault("supportServprodNames", "")==null?"":json.getOrDefault("supportServprodNames", "").toString();
		String couponAmount = json.getOrDefault("couponAmount", "")==null?"":json.getOrDefault("couponAmount", "").toString();
		if(StringUtils.isBlank(couponAmount)) {
			this.reduce_cost = Lists.newArrayList("0","0");
		}else {
			BigDecimal amount = new BigDecimal(couponAmount);
			if("DISCOUNT".equalsIgnoreCase(card_type)) {
				amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
				this.discount = Lists.newArrayList(one+"",two+"");
			}else{
				BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
				BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
				two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
				this.reduce_cost = Lists.newArrayList(one+"",(two+"").replaceAll("\\.", ""));
			}
		}
	}
}
